package free.emaranhado.casadolago.parser;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import free.emaranhado.casadolago.listener.XMLParserListener;
import free.emaranhado.casadolago.model.Category;

import android.util.Log;

public class CategoriesXMLParser extends DefaultHandler {
	Boolean currentElement = false;
	String currentValue = null;
	private Category mCategory = new Category();
	private XMLParserListener mListener;

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {

		currentElement = true;

		if (localName.equals("Eventos")) {
		}
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {

		currentElement = false;

		Log.i("XML", "=> " + localName + " = " + currentValue);
		if (localName.equalsIgnoreCase("Name")) {
			mCategory.setName(currentValue);
		} else if (localName.equalsIgnoreCase("idCategory")) {
			mCategory.setIdCategory(currentValue);
		} else if (localName.equalsIgnoreCase("NumEventos")) {
			mCategory.setNumEventos(currentValue);
		}
		else if (localName.equalsIgnoreCase("Categoria")) {
			mListener.onRequestFinish(mCategory);
			mCategory = new Category();
		}
	}

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {

		if (currentElement) {
			currentValue = new String(ch, start, length);
			currentElement = false;
		}

	}

	public void setListener(XMLParserListener listener) {
		mListener = listener;
		
	}
}
